// Library.cpp : Defines the entry point for the DLL application.
//

#include "Library.h"
#include "camera.h"

std::vector<Camera *> handles;


#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
    return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

// __stdcall
void * __cdecl initialize(int camera_index)
{
	Camera * ret = NULL;
	try {
		ret = new Camera(camera_index);
		handles.push_back(ret);
	}
	catch(...) {
		return NULL;
	}

	return ret;
}

int __cdecl UNinitialize(Camera * device)
{
	delete device;
	return 1;
}

CameraResult __cdecl run_camera(void * subject)
{
	Camera * camera = (Camera *) subject;
	for(unsigned int i=0; i<handles.size(); i++)
	if (camera == handles[i])
	{
		double ts = camera->time.seconds();
		camera->process();
		ts = camera->time.seconds() - ts;
		int key = cvWaitKey(10); // for displaying windows
		CameraResult ret;
		ret.status = 1;
		ret.operation_interval = ts;
		return ret;
	}
	return CameraResult();
}

int __cdecl add_filter_to_camera(Camera * subject, RawPixel object, Filter filter)
{
	subject->add_filter(object, filter);
	return 1;
}

int __cdecl clear_filters(Camera * subject)
{
	subject->clear_all_filters();
	return 1;
}

MouseEvent __cdecl get_mouse_event(Camera * subject)
{
	return subject->get_event();
}


